package dS.ProjectGPS.LeicaOWI;

import java.util.Vector;
import javax.microedition.lcdui.*;

public class FormOwiCMD extends Form implements FormOwi {

  OWIAgent owiAgent = null;

  TextField configType = new TextField("Configuration Type", "", 20,
                                       TextField.NUMERIC);
  ChoiceGroup fileCreation = new ChoiceGroup("File Creation", ChoiceGroup.POPUP);
  ChoiceGroup fileDeletion = new ChoiceGroup("File Deletion", ChoiceGroup.POPUP);
  StringItem note = new StringItem("",
                                   "Variables below valid only if type is '0'",
                                   StringItem.PLAIN);
  StringItem noteFC = new StringItem("", "If file creation is on.",
                                     StringItem.PLAIN);
  ChoiceGroup timeIntervalCreate = new ChoiceGroup("Time Interval",
     ChoiceGroup.POPUP);
  Spacer spacer1 = new Spacer(0, 0);
  ChoiceGroup splitTracks = new ChoiceGroup("Split Files", ChoiceGroup.POPUP);
  StringItem noteFD = new StringItem("", "If deletion is on.", StringItem.PLAIN);
  ChoiceGroup timeIntervalDelete = new ChoiceGroup("Time Interval",
     ChoiceGroup.POPUP);
  ChoiceGroup mdb = new ChoiceGroup("MDB Creation", ChoiceGroup.POPUP);
  ChoiceGroup fileSeq = new ChoiceGroup("File Sequence", ChoiceGroup.POPUP);

  public FormOwiCMD(OWIAgent o) {
    super("Measurement Data");

    owiAgent = o;

    try {
      ndsInit();
    }
    catch (Exception ex) {
    }
  }

  public void ndsInit() throws Exception {
    append(configType);
    append(note);
    append(fileCreation);
    fileCreation.append("Off", null);
    fileCreation.append("On", null);
    append(noteFC);
    append(timeIntervalCreate);
    append(splitTracks);
    append(spacer1);
    append(fileDeletion);
    fileDeletion.append("Off", null);
    fileDeletion.append("On", null);
    timeIntervalCreate.append("10 min", null);
    timeIntervalCreate.append("30 min", null);
    timeIntervalCreate.append("1 hr", null);
    timeIntervalCreate.append("2 hr", null);
    timeIntervalCreate.append("4 hr", null);
    timeIntervalCreate.append("6 hr", null);
    timeIntervalCreate.append("12 hr", null);
    timeIntervalCreate.append("24 hr", null);
    spacer1.setPreferredSize(167, 10);
    splitTracks.append("After track end", null);
    splitTracks.append("Regardless of track end", null);
    append(noteFD);
    append(timeIntervalDelete);
    timeIntervalDelete.append("1 Day", null);
    timeIntervalDelete.append("2 Days", null);
    timeIntervalDelete.append("7 Days", null);
    timeIntervalDelete.append("1 Month", null);
    append(fileSeq);
    append(mdb);
    fileSeq.append("Off", null);
    fileSeq.append("On", null);
    mdb.append("Off", null);
    mdb.append("On", null);
  }

  public void update() {
    owiAgent.query(OWI.qsCMD(configType.getString()));

    configType.setString("" + OWI.cmdConfigType);
    fileCreation.setSelectedIndex(OWI.cmdContFileCreation, true);
    fileDeletion.setSelectedIndex(OWI.cmdContFileDeletion, true);
    timeIntervalCreate.setSelectedIndex(OWI.cmdTimeIntervalCreate, true);
    splitTracks.setSelectedIndex(OWI.cmdSplitTrack, true);
    timeIntervalDelete.setSelectedIndex(OWI.cmdTimeIntervalDelete, true);
    fileSeq.setSelectedIndex(OWI.cmdFileSeq, true);
    mdb.setSelectedIndex(OWI.cmdMDBFileStats, true);
  }

  public void save() {

    String msgId = "CMD";

    Vector outputs = new Vector();

    // Setup SET parameters
    outputs.addElement(msgId);
    outputs.addElement(configType.getString());

    if(configType.getString() == "0"){
      outputs.addElement("" + fileCreation.getSelectedIndex());
      outputs.addElement("" + fileDeletion.getSelectedIndex());

      if(fileCreation.getSelectedIndex() == 1){
        outputs.addElement("" + timeIntervalCreate.getSelectedIndex());
        outputs.addElement("" + splitTracks.getSelectedIndex());
      }

      if(fileDeletion.getSelectedIndex() == 1){
        outputs.addElement("" + timeIntervalDelete.getSelectedIndex());
        outputs.addElement("" + fileSeq.getSelectedIndex());
         outputs.addElement("" + mdb.getSelectedIndex());
      }
    }

    String[] outputSA = new String[outputs.size()];
    for(int i=0; i<outputs.size(); i++){
      outputSA[i] = (String) outputs.elementAt(i);
    }

    //Send message and update form if successful
    int ackCode = (owiAgent.set(msgId, OWI.setMessage(outputSA)));
    Controller.showACKAlert(this, this, ackCode);
  }

}
